package com.cqw.auth.handlers;

import com.cqw.auth.service.TokenService;
import com.cqw.domain.SysUser;
import com.cqw.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-07-19 10:41
 * @Description: 退出处理类
 */
@Component
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {


    @Autowired
    private TokenService tokenService;


    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        SysUser loginUser = tokenService.getLoginUser(request);
        if (loginUser != null) {
            //删除用户身份信息
            tokenService.delLoginUser(loginUser.getToken());
        }
        ServletUtils.sendString(response, "退出成功");
    }
}
